//
//  SceneManager.m
//  GameProject
//
//  Created by Blair Trusler on 20/10/2011.
//  Copyright (c) 2011 Blair Trusler. All rights reserved.
//

#import "GameHeader.h"

@implementation SceneManager

static SceneManager* _sharedSceneManager = nil;                      

+(SceneManager*)sharedSceneManager {
    @synchronized([SceneManager class])                             
    {
        if(!_sharedSceneManager)                                    
            [[self alloc] init]; 
        return _sharedSceneManager;                                 
    }
    return nil; 
}

+(id)alloc 
{
    @synchronized ([SceneManager class])                            
    {
        NSAssert(_sharedSceneManager == nil, @"Attempted to allocated a second instance of the Game Manager singleton"); // 6
        _sharedSceneManager = [super alloc];
        return _sharedSceneManager;                                 
    }
    return nil;  
}


- (NSString*)formatSceneTypeToString:(SceneTypes)sceneID {
    NSString *result = nil;
    switch(sceneID) {
        case kNoSceneUninitialized:
            result = @"kNoSceneUninitialized";
            break;
        case kMainMenuScene:
            result = @"kMainMenuScene";
            break;
        case kLevelSelectScene:
            result = @"kLevelSelectScene";
            break;
        case kOptionsScene:
            result = @"kOptionsScene";
            break;
        case kCreditsScene:
            result = @"kCreditsScene";
            break;
        case kGuideScene:
            result = @"kGuideScene";
            break;
        case kLevelCompleteScene:
            result = @"kLevelCompleteScene";
            break;
        case kGameOverScene:
            result = @"kGameOverScene";
            break;
        case kGameLevel1:
            result = @"kGameLevel1";
            break;
        case kGameLevel2:
            result = @"kGameLevel2";
            break;
        case kGameLevel3:
            result = @"kGameLevel3";
            break;
        case kGameLevel4:
            result = @"kGameLevel4";
            break;
        case kGameLevel5:
            result = @"kGameLevel5";
            break;
        default:
            [NSException raise:NSGenericException format:@"Unexpected SceneType."];
    }
    return result;
}

-(id)init {                                                       
    self = [super init];
    if (self != nil) {
        CCLOG(@"Scene Manager Singleton initialized");
        currentScene = kNoSceneUninitialized;
    }
    return self;
}


-(void)runSceneWithID:(SceneTypes)sceneID {
    SceneTypes previousScene = currentScene;
    currentScene = sceneID;
    
    id sceneToRun = nil;
    switch (sceneID) {
        case kMainMenuScene: 
            sceneToRun = [MainMenuScene node];
            break;
        case kOptionsScene:
            sceneToRun = [StatMenuScene node];
            break;
        case kLevelSelectScene:
            sceneToRun = [LevelSelect node];
            break;
        case kCreditsScene:
            sceneToRun = [Credits node];
            break;
        case kGuideScene:
            sceneToRun = [GuideScene node];
            break;
        case kGameOverScene:
            sceneToRun = [GameOver node];
            break;
        case kLevelCompleteScene:
            sceneToRun = [LevelComplete node];
            break;
        case kGameLevel1: 
            sceneToRun = [Game node];
            break;
        case kGameLevel2:
            sceneToRun = [Level2Scene node];
            break;
        case kGameLevel3:
            sceneToRun = [Level3Scene node];
            break;
        case kGameLevel4:
            sceneToRun = [Level4Scene node];
            break;
        case kGameLevel5:
            sceneToRun = [Level5Scene node];
            break;

        default:
            CCLOG(@"Unknown ID, cannot switch scenes");
            return;
            break;
    }
    
    if (sceneToRun == nil) {
        // Revert back, since no new scene was found
        currentScene = previousScene;
        return;
    }

    
    if ([[CCDirector sharedDirector] runningScene] == nil) {
        [[CCDirector sharedDirector] runWithScene:sceneToRun];
        
    } else {
        
        [[CCDirector sharedDirector] replaceScene:sceneToRun];
    }
    
}


@end
